import _daoFactory from '../factory/DaoFactory'

import Config from '../Config'
import { EventEmitter } from 'events';

import _EngineDefined from './EngineDefined';
import MySQLExecutor from './mysql/MySQLExecutor';
import IExecutor from './IExecutor'

class EntityAgent extends EventEmitter {
    entitys = [];
    _DbExecutor: IExecutor = null;

    constructor() {
        super();
        let dbEngine = Config.dao.dbEngine;
        let _session = _daoFactory.createDao(dbEngine);
        switch (dbEngine.toLowerCase()) {
            case _EngineDefined.MySQL.toLowerCase():
                this._DbExecutor = new MySQLExecutor(_session)
                break;
            case _EngineDefined.MongoDB.toLowerCase():
                // this._DbExecutor = new MySQLExecutor(_session)
                break;
            default: break;
        }

        if (this._DbExecutor == null) {
            throw new Error('expected a db engine,but none')
        }

        this.on("_ENTITY_SAVE", this._DbExecutor.executeSaveSQL.bind(this._DbExecutor))
        this.on("_ENTITY_DELETE", this._DbExecutor.executeDeleteSQL.bind(this._DbExecutor))
        this.on("_ENTITY_DDL_UPDATE", this._DbExecutor.executeUpdateDDL.bind(this._DbExecutor))
        this.on("_ENTITY_FILL",this._DbExecutor.executeQuerySQL.bind(this._DbExecutor))
    }


}

let _EntityAgent = new EntityAgent();
export default _EntityAgent;